package org.dce.screencapturer.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

@SuppressWarnings("serial")
public class ColorChooser 
	extends JTabbedPane{

	private static final Color[] DEFAULT_COLORS = {
		Color.BLACK,
		new Color(0x000080),new Color(0x008000),new Color(0x008080),
		new Color(0x800000),new Color(0x800080),new Color(0x808000),
		new Color(0x808080),new Color(0xc0c0c0),new Color(0x0000ff),
		new Color(0x00ff00),new Color(0x00ffff),new Color(0xff0000),
		new Color(0xff00ff),new Color(0xffff00),new Color(0xffffff),
		new Color(0x333333),new Color(0x4c4c4c),new Color(0x666666),
		new Color(0x999999),new Color(0xb3b3b3),new Color(0xcccccc),
		new Color(0xe6e6e6),new Color(0xe6e6ff),new Color(0xff3366),
		new Color(0xdc2300),new Color(0xb84700),new Color(0xff3333),
		new Color(0xeb613d),new Color(0xb84747),new Color(0xb80047),
		new Color(0x99284c),new Color(0x94006b),new Color(0x94476b),
		new Color(0x944794),new Color(0x9966ee),new Color(0x6b4794),
		new Color(0x6b2394),new Color(0x6b0094),new Color(0x5e11a6),
		new Color(0x280099),new Color(0x4700b8),new Color(0x2300dc),
		new Color(0x2323dc),new Color(0x0047ff),new Color(0x0099ff),
		new Color(0x00b8ff),new Color(0x99ccff),new Color(0x00dcff),
		new Color(0x00cccc),new Color(0x23b8dc),new Color(0x47b8b8),
		new Color(0x33a3a3),new Color(0x198a8a),new Color(0x006b6b),
		new Color(0x004a4a),new Color(0x355c00),new Color(0x5c8526),
		new Color(0x7da647),new Color(0x94bd5c),new Color(0x00ae00),
		new Color(0x33cc66),new Color(0x3dcb3d),new Color(0x23ff23),
		new Color(0xe6ff00),new Color(0xffff99),new Color(0xffff66),
		new Color(0xe6e64c),new Color(0xcccc00),new Color(0xb3b300),
		new Color(0x808019),new Color(0x666600),new Color(0x4c1900),
		new Color(0x663300),new Color(0x804c19),new Color(0x996633),
		new Color(0xcc6633),new Color(0xff6633),new Color(0xff9966),
		new Color(0xffcc99),new Color(0x9999ff),new Color(0x993366),
		new Color(0xffffcc),new Color(0xccffff),new Color(0x660066),
		new Color(0xff8080),new Color(0x0066cc),new Color(0x333366),
		new Color(0x666699),new Color(0x9999cc),new Color(0xccccff),
		new Color(0xff6309),new Color(0xffb515),new Color(0xc90016),
		new Color(0x004586),new Color(0xff420e),new Color(0xffd320),
		new Color(0x579d1c),new Color(0x7e0021),new Color(0x83caff),
		new Color(0x314004),new Color(0xaecf00),new Color(0x4b1f6f),
		new Color(0xff950e),new Color(0xc5000b),new Color(0x0084d1)
	};
	
	private JColorChooser chooser = new JColorChooser();
	private ColorMatrix availableColors = new ColorMatrix(DEFAULT_COLORS);
	private ColorMatrix recentlyAddedColors = new ColorMatrix(16);
	
	public ColorChooser(){
		JPanel defaultColors = new JPanel(new BorderLayout());
		availableColors.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(),"Default colors"));
		defaultColors.add(availableColors,BorderLayout.CENTER);
		recentlyAddedColors.setBorder(new TitledBorder(BorderFactory.createEtchedBorder(),"Recently used"));
		defaultColors.add(recentlyAddedColors,BorderLayout.SOUTH);
		addTab("Default colors", defaultColors);
		addTab("Custom color", chooser);
	}
	
	private class ColorMatrix extends JPanel 
		implements MouseListener{
		
		public ColorMatrix(int maxColors){
			Color[] colors = new Color[maxColors];
			for (int i=0;i<colors.length;i++){
				colors[i] = Color.WHITE;
			}
			buildMaxtrix(colors);
		}
		
		public ColorMatrix(Color[] colors){
			buildMaxtrix(colors);
		}
		
		private void buildMaxtrix(Color[] colors){
			setLayout(new FlowLayout(FlowLayout.LEFT, 3, 3));
			for (Color color:colors){
				JLabel button = new JLabel();
				button.setPreferredSize(new Dimension(24,24));
				button.setMinimumSize(new Dimension(24,24));
				button.setOpaque(true);
				button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
				button.setBackground(color);
				button.addMouseListener(this);
				add(button);
			}
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			
		}

		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {}
		public void mouseReleased(MouseEvent e) {}
	}
	
	public static void main(String[] argv) throws Exception{
		
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		JFrame f = new JFrame();
		f.add(new ColorChooser());
		f.setSize(300,300);
		f.setVisible(true);
	}
}
